home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / bfd / archures.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  14KB  |  607 lines

  1. /* BFD library support routines for architectures.
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.    Hacked by John Gilmore and Steve Chamberlain of Cygnus Support.
  4.  
  5.  
  6. This file is part of BFD, the Binary File Descriptor library.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. /*doc*
  23. @section Architectures
  24. BFD's idea of an architecture is implimented in @code{archures.c}. BFD
  25. keeps one atom in a BFD describing the architecture of the data
  26. attached to the BFD;  a pointer to a @code{bfd_arch_info_type}. 
  27.  
  28. Pointers to structures can be requested independently of a bfd so that
  29. an architecture's information can be interrogated without access to an
  30. open bfd.
  31.  
  32. The arch information is provided by each architecture package.  The
  33. set of default architectures is selected by the #define
  34. @code{SELECT_ARCHITECTURES}.  This is normally set up in the
  35. @code{config\/h\-} file of your choice.  If the name is not defined,
  36. then all the architectures supported are included.
  37.  
  38. When BFD starts up, all the architectures are called with an
  39. initialize method.  It is up to the architecture back end to insert as
  40. many items into the list of arches as it wants to, generally this
  41. would be one for each machine and one for the default case (an item
  42. with a machine field of 0).
  43.  
  44. */
  45.  
  46. /*proto* bfd_architecture
  47. This enum gives the object file's CPU
  48. architecture, in a global sense.  E.g. what processor family does it
  49. belong to?  There is another field, which indicates what processor
  50. within the family is in use.  The machine gives a number which
  51. distingushes different versions of the architecture, containing for
  52. example 2 and 3 for Intel i960 KA and i960 KB, and 68020 and 68030 for
  53. Motorola 68020 and 68030.
  54.  
  55. *+
  56. enum bfd_architecture 
  57. {
  58.   bfd_arch_unknown,   {* File arch not known *}
  59.   bfd_arch_obscure,   {* Arch known, not one of these *}
  60.   bfd_arch_m68k,      {* Motorola 68xxx *}
  61.   bfd_arch_vax,       {* DEC Vax *}   
  62.   bfd_arch_i960,      {* Intel 960 *}
  63.     {* The order of the following is important.
  64.        lower number indicates a machine type that 
  65.        only accepts a subset of the instructions
  66.        available to machines with higher numbers.
  67.        The exception is the "ca", which is
  68.        incompatible with all other machines except 
  69.        "core". *}
  70.  
  71. #define bfd_mach_i960_core      1
  72. #define bfd_mach_i960_ka_sa     2
  73. #define bfd_mach_i960_kb_sb     3
  74. #define bfd_mach_i960_mc        4
  75. #define bfd_mach_i960_xa        5
  76. #define bfd_mach_i960_ca        6
  77.  
  78.   bfd_arch_a29k,      {* AMD 29000 *}
  79.   bfd_arch_sparc,     {* SPARC *}
  80.   bfd_arch_mips,      {* MIPS Rxxxx *}
  81.   bfd_arch_i386,      {* Intel 386 *}
  82.   bfd_arch_ns32k,     {* National Semiconductor 32xxx *}
  83.   bfd_arch_tahoe,     {* CCI/Harris Tahoe *}
  84.   bfd_arch_i860,      {* Intel 860 *}
  85.   bfd_arch_romp,      {* IBM ROMP RS/6000 *}
  86.   bfd_arch_alliant,   {* Alliant *}
  87.   bfd_arch_convex,    {* Convex *}
  88.   bfd_arch_m88k,      {* Motorola 88xxx *}
  89.   bfd_arch_pyramid,   {* Pyramid Technology *}
  90.   bfd_arch_h8300,     {* Hitachi H8/300 *}
  91.   bfd_arch_last
  92.   };
  93. *-
  94.  
  95. stuff
  96.  
  97. */
  98.  
  99.  
  100.  
  101. /* $Id: archures.c,v 1.17 1991/10/11 10:05:21 gnu Exp $ */
  102.  
  103. #include "bfd.h"
  104. #include "sysdep.h"
  105. #include "libbfd.h"
  106.  
  107. /*proto* bfd_arch_info
  108. This structure contains information on architectures.
  109. *+
  110. typedef int bfd_reloc_code_type;
  111.  
  112. typedef struct bfd_arch_info 
  113. {
  114.   int bits_per_word;
  115.   int bits_per_address;
  116.   int bits_per_byte;
  117.   enum bfd_architecture arch;
  118.   long mach;
  119.   char *arch_name;
  120.   CONST  char *printable_name;
  121. {* true if this is the default machine for the architecture *}
  122.   boolean the_default;    
  123.   CONST struct bfd_arch_info * EXFUN((*compatible),(CONST struct bfd_arch_info *a,
  124.                              CONST struct bfd_arch_info *b));
  125.  
  126.   boolean EXFUN((*scan),(CONST struct bfd_arch_info *,CONST char *));
  127.   unsigned int EXFUN((*disassemble),(bfd_vma addr, CONST char *data,
  128.                      PTR stream));
  129.   CONST struct reloc_howto_struct *EXFUN((*reloc_type_lookup), (CONST struct
  130.                                 bfd_arch_info *,
  131.                                 bfd_reloc_code_type  code));
  132.  
  133.   struct bfd_arch_info *next;
  134.  
  135. } bfd_arch_info_type;
  136.  
  137.  
  138. *-
  139. */
  140.  
  141. bfd_arch_info_type   *bfd_arch_info_list;
  142.  
  143.  
  144. /*proto* bfd_printable_name
  145.  
  146. Return a printable string representing the architecture and machine
  147. from the pointer to the arch info structure 
  148.  
  149. *; CONST char *EXFUN(bfd_printable_name,(bfd *abfd));
  150.  
  151. */
  152.  
  153. CONST char *
  154. DEFUN(bfd_printable_name, (abfd),
  155.       bfd *abfd) 
  156. {
  157.   return abfd->arch_info->printable_name;
  158. }
  159.  
  160.  
  161.  
  162. /*proto*
  163. *i bfd_scan_arch
  164. This routine is provided with a string and tries to work out if bfd
  165. supports any cpu which could be described with the name provided.  The
  166. routine returns a pointer to an arch_info structure if a machine is
  167. found, otherwise NULL.
  168.  
  169. *; bfd_arch_info_type *EXFUN(bfd_scan_arch,(CONST char *));
  170. */
  171.  
  172. bfd_arch_info_type *
  173. DEFUN(bfd_scan_arch,(string),
  174.       CONST char *string)
  175. {
  176.   struct bfd_arch_info *ap;
  177.  
  178.   /* Look through all the installed architectures */
  179.   for (ap = bfd_arch_info_list;
  180.        ap != (bfd_arch_info_type *)NULL;
  181.        ap = ap->next) {
  182.  
  183.     if (ap->scan(ap, string)) 
  184.       return ap;
  185.   }
  186.   return (bfd_arch_info_type *)NULL;
  187. }
  188.  
  189.  
  190.  
  191. /*proto* bfd_arch_get_compatible
  192. This routine is used to determine whether two BFDs' architectures and
  193. machine types are compatible.  It calculates the lowest common
  194. denominator between the two architectures and machine types implied by
  195. the BFDs and returns a pointer to an arch_info structure describing
  196. the compatible machine.
  197.  
  198. *; CONST bfd_arch_info_type *EXFUN(bfd_arch_get_compatible,
  199.      (CONST bfd *abfd,
  200.      CONST bfd *bbfd));
  201. */
  202.  
  203. CONST bfd_arch_info_type *
  204. DEFUN(bfd_arch_get_compatible,(abfd, bbfd),
  205. CONST    bfd *abfd AND
  206. CONST    bfd *bbfd)
  207.  
  208. {
  209.   return  abfd->arch_info->compatible(abfd->arch_info,bbfd->arch_info);
  210. }
  211.  
  212.  
  213. /*proto-internal* bfd_default_arch_struct
  214.  
  215. What bfds are seeded with 
  216.  
  217. *+
  218. extern bfd_arch_info_type bfd_default_arch_struct;
  219. *-
  220. */
  221.  
  222. bfd_arch_info_type bfd_default_arch_struct =
  223.   {
  224.     32,32,8,bfd_arch_unknown,0,"unknown","unknown",true,
  225.     bfd_default_compatible, bfd_default_scan,
  226.  
  227.   };
  228.  
  229. /*proto* bfd_set_arch_info
  230.  
  231. *; void EXFUN(bfd_set_arch_info,(bfd *, bfd_arch_info_type *));
  232.  
  233. */
  234.  
  235. void DEFUN(bfd_set_arch_info,(abfd, arg),
  236. bfd *abfd AND
  237. bfd_arch_info_type *arg)
  238. {
  239.   abfd->arch_info = arg;
  240. }
  241.  
  242. /*proto-internal* bfd_default_set_arch_mach
  243.  
  244. Set the architecture and machine type in a bfd. This finds the correct
  245. pointer to structure and inserts it into the arch_info pointer. 
  246.  
  247. *;  boolean EXFUN(bfd_default_set_arch_mach,(bfd *abfd,
  248.           enum bfd_architecture arch,
  249.      unsigned long mach));
  250.  
  251. */
  252.  
  253. boolean DEFUN(bfd_default_set_arch_mach,(abfd, arch, mach),
  254.           bfd *abfd AND
  255.           enum bfd_architecture arch AND
  256.           unsigned    long mach)
  257. {
  258.   static struct bfd_arch_info *old_ptr = &bfd_default_arch_struct;
  259.   boolean found = false;
  260.   /* run through the table to find the one we want, we keep a little
  261.      cache to speed things up */
  262.   if (old_ptr == 0 || arch != old_ptr->arch || mach != old_ptr->mach) {
  263.     bfd_arch_info_type *ptr;
  264.     old_ptr = (bfd_arch_info_type *)NULL;
  265.     for (ptr = bfd_arch_info_list;
  266.      ptr != (bfd_arch_info_type *)NULL;
  267.      ptr= ptr->next) {
  268.       if (ptr->arch == arch &&
  269.       ((ptr->mach == mach) || (ptr->the_default && mach == 0))) {
  270.     old_ptr = ptr;
  271.     found = true;
  272.     break;
  273.       }
  274.     }
  275.     if (found==false) {
  276.       /*looked for it and it wasn't there, so put in the default */
  277.       old_ptr = &bfd_default_arch_struct;
  278.  
  279.     }
  280.   }
  281.   else {
  282.     /* it was in the cache */
  283.     found = true;
  284.   }
  285.  
  286.   abfd->arch_info = old_ptr;
  287.  
  288.   return found;
  289. }
  290.  
  291.  
  292.  
  293.  
  294.  
  295. /*proto* bfd_get_arch
  296.  
  297. Returns the enumerated type which describes the supplied bfd's
  298. architecture
  299.  
  300. *; enum bfd_architecture EXFUN(bfd_get_arch, (bfd *abfd));
  301. */
  302.  
  303.  enum bfd_architecture DEFUN(bfd_get_arch, (abfd), bfd *abfd)
  304.   {
  305.     return abfd->arch_info->arch;
  306.  
  307.  
  308.   }
  309.  
  310. /*proto* bfd_get_mach
  311.  
  312. Returns the long type which describes the supplied bfd's
  313. machine
  314.  
  315. *; unsigned long EXFUN(bfd_get_mach, (bfd *abfd));
  316. */
  317.  
  318. unsigned long  DEFUN(bfd_get_mach, (abfd), bfd *abfd)
  319. {
  320.     return abfd->arch_info->mach;
  321.   }
  322.  
  323. /*proto* bfd_arch_bits_per_byte
  324.  
  325. Returns the number of bits in one of the architectures bytes
  326.  
  327. *; unsigned int EXFUN(bfd_arch_bits_per_byte, (bfd *abfd));
  328. */
  329.  
  330. unsigned int DEFUN(bfd_arch_bits_per_byte, (abfd), bfd *abfd)
  331.   {
  332.     return abfd->arch_info->bits_per_byte;
  333.   }
  334.  
  335. /*proto* bfd_arch_bits_per_address
  336.  
  337. Returns the number of bits in one of the architectures addresses
  338.  
  339. *; unsigned int EXFUN(bfd_arch_bits_per_address, (bfd *abfd));
  340. */
  341.  
  342. unsigned int DEFUN(bfd_arch_bits_per_address, (abfd), bfd *abfd)
  343.   {
  344.     return abfd->arch_info->bits_per_address;
  345.   }
  346.  
  347.  
  348.  
  349. extern void EXFUN(bfd_h8300_arch,(void));
  350. extern void EXFUN(bfd_i960_arch,(void));
  351. extern void EXFUN(bfd_empty_arch,(void));
  352. extern void EXFUN(bfd_sparc_arch,(void));
  353. extern void EXFUN(bfd_m88k_arch,(void));
  354. extern void EXFUN(bfd_m68k_arch,(void));
  355. extern void EXFUN(bfd_vax_arch,(void));
  356. extern void EXFUN(bfd_a29k_arch,(void));
  357. extern void EXFUN(bfd_mips_arch,(void));
  358. extern void EXFUN(bfd_i386_arch,(void));
  359.  
  360.  
  361.  
  362. static void EXFUN((*archures_init_table[]),()) = 
  363. {
  364. #ifdef SELECT_ARCHITECTURES
  365.   SELECT_ARCHITECTURES,
  366. #else
  367.   bfd_sparc_arch,
  368.   bfd_a29k_arch,
  369.   bfd_mips_arch,
  370.   bfd_h8300_arch,
  371.   bfd_i386_arch,
  372.   bfd_m88k_arch,
  373.   bfd_i960_arch,
  374.   bfd_m68k_arch,
  375.   bfd_vax_arch,
  376. #endif
  377.   0
  378.   };
  379.  
  380.  
  381.  
  382. /*proto-internal*
  383.  
  384. This routine initializes the architecture dispatch table by calling
  385. all installed architecture packages and getting them to poke around.
  386.  
  387. *; PROTO(void, bfd_arch_init,(void));
  388.  
  389. */
  390.  
  391. void
  392. DEFUN_VOID(bfd_arch_init)
  393. {
  394.   void EXFUN((**ptable),());
  395.   for (ptable = archures_init_table; 
  396.        *ptable ;
  397.        ptable++)
  398.       {
  399.     (*ptable)();
  400.       }
  401. }
  402.  
  403.  
  404. /*proto-internal* bfd_arch_linkin
  405.  
  406. Link the provided arch info structure into the list
  407.  
  408. *; void EXFUN(bfd_arch_linkin,(bfd_arch_info_type *));
  409.  
  410. */
  411.  
  412. void DEFUN(bfd_arch_linkin,(ptr),
  413.        bfd_arch_info_type *ptr)
  414. {
  415.   ptr->next = bfd_arch_info_list;
  416.   bfd_arch_info_list = ptr;
  417. }
  418.  
  419.  
  420. /*proto-internal* bfd_default_compatible
  421.  
  422. The default function for testing for compatibility 
  423.  
  424. *; CONST bfd_arch_info_type *EXFUN(bfd_default_compatible,
  425.      (CONST bfd_arch_info_type *a,
  426.      CONST bfd_arch_info_type *b));
  427. */
  428.  
  429. CONST bfd_arch_info_type *
  430. DEFUN(bfd_default_compatible,(a,b),
  431.       CONST bfd_arch_info_type *a AND
  432.       CONST bfd_arch_info_type *b)
  433. {
  434.   if(a->arch != b->arch) return NULL;
  435.  
  436.   if (a->mach > b->mach) {
  437.     return a;
  438.   }
  439.   if (b->mach > a->mach) {
  440.     return b;
  441.   }
  442.   return a;
  443. }
  444.  
  445. /*proto-internal* bfd_default_scan
  446. The default function for working out whether this is an architecture
  447. hit and a machine hit 
  448.  
  449. *; boolean EXFUN(bfd_default_scan,(CONST struct bfd_arch_info *, CONST char *));
  450.  
  451. */
  452.  
  453. boolean 
  454. DEFUN(bfd_default_scan,(info, string),
  455. CONST struct bfd_arch_info *info AND
  456. CONST char *string)
  457. {
  458.   CONST  char *ptr_src;
  459.   CONST   char *ptr_tst;
  460.   unsigned long number;
  461.   enum bfd_architecture arch;
  462.   /* First test for an exact match */
  463.   if (strcmp(string, info->printable_name) == 0) return true;
  464.  
  465.   /* See how much of the supplied string matches with the
  466.      architecture, eg the string m68k:68020 would match the 68k entry
  467.      up to the :, then we get left with the machine number */
  468.  
  469.   for (ptr_src = string,
  470.        ptr_tst = info->arch_name; 
  471.        *ptr_src && *ptr_tst;
  472.        ptr_src++,
  473.        ptr_tst++) 
  474.       {
  475.     if (*ptr_src != *ptr_tst) break;
  476.       }
  477.  
  478.   /* Chewed up as much of the architecture as will match, skip any
  479.      colons */
  480.   if (*ptr_src == ':') ptr_src++;
  481.   
  482.   if (*ptr_src == 0) {
  483.     /* nothing more, then only keep this one if it is the default
  484.        machine for this architecture */
  485.     return info->the_default;
  486.   }
  487.   number = 0;
  488.   while (isdigit(*ptr_src)) {
  489.     number = number * 10 + *ptr_src  - '0';
  490.     ptr_src++;
  491.   }
  492.  
  493.   switch (number) {
  494.   case 68010:
  495.   case 68020:
  496.   case 68030:
  497.   case 68040:
  498.   case 68332:
  499.   case 68050:        
  500.   case 68000: 
  501.     arch = bfd_arch_m68k; 
  502.     break;
  503.   case 386: 
  504.   case 80386:
  505.   case 486:
  506.     arch = bfd_arch_i386;
  507.     break;
  508.   case 29000: 
  509.     arch = bfd_arch_a29k;
  510.     break;
  511.  
  512.   case 32016:
  513.   case 32032:
  514.   case 32132:
  515.   case 32232:
  516.   case 32332:
  517.   case 32432:
  518.   case 32532:  
  519.   case 32000: 
  520.     arch = bfd_arch_ns32k; 
  521.     break;
  522.  
  523.   case 860:
  524.   case 80860: 
  525.     arch = bfd_arch_i860; 
  526.     break;
  527.  
  528.   default:  
  529.     return false;
  530.   }
  531.   if (arch != info->arch) 
  532.     return false;
  533.  
  534.   if (number != info->mach)
  535.     return false;
  536.  
  537.   return true;
  538. }
  539.  
  540.  
  541.  
  542.  
  543. /*proto* bfd_get_arch_info
  544.  
  545. *; bfd_arch_info_type * EXFUN(bfd_get_arch_info,(bfd *));
  546.  
  547. */
  548.  
  549. bfd_arch_info_type *
  550. DEFUN(bfd_get_arch_info,(abfd),
  551. bfd *abfd)
  552. {
  553.   return  abfd->arch_info;
  554. }
  555.  
  556.  
  557. /*proto* bfd_lookup_arch
  558.  
  559. *; bfd_arch_info_type * EXFUN(bfd_lookup_arch,(enum
  560.     bfd_architecture arch,long machine));
  561.  
  562. Look for the architecure info struct which matches the arguments
  563. given. A machine of 0 will match the machine/architecture structure which
  564. marks itself as the default.
  565.  
  566. */
  567.  
  568. bfd_arch_info_type * 
  569. DEFUN(bfd_lookup_arch,(arch, machine),
  570. enum bfd_architecture arch AND
  571. long machine)
  572. {
  573.   bfd_arch_info_type *ap;
  574.   bfd_check_init();  
  575.   for (ap = bfd_arch_info_list; 
  576.        ap !=  (bfd_arch_info_type *)NULL;
  577.        ap = ap->next) {
  578.     if (ap->arch == arch &&
  579.     ((ap->mach == machine) || (ap->the_default && machine == 0))) {
  580.       return ap;
  581.     }
  582.   }
  583.   return (bfd_arch_info_type *)NULL;
  584. }
  585.  
  586.  
  587.  
  588. /*proto* bfd_printable_arch_mach
  589. Return a printable string representing the architecture and machine
  590. type. 
  591.  
  592. NB. The use of this routine is depreciated.
  593.  
  594. *; PROTO(CONST char *,bfd_printable_arch_mach,
  595.     (enum bfd_architecture arch, unsigned long machine));
  596. */
  597.  
  598. CONST char *
  599. DEFUN(bfd_printable_arch_mach,(arch, machine),
  600.       enum bfd_architecture arch AND
  601.       unsigned long machine)
  602. {
  603.   bfd_arch_info_type *ap = bfd_lookup_arch(arch, machine);
  604.   if(ap) return ap->printable_name;
  605.   return "UNKNOWN!";
  606. }
  607.